package com.jhp.eureka.controller;

import com.jhp.eureka.balancer.LoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class HelloController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancer balancer;

    //    @GetMapping(value = "/hello")
//    public String hello(@RequestParam String name) {
////        //获取服务列表;
////        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-client-provider");
////        //获取第一个;
////        ServiceInstance instance = instances.get(0);
////        return restTemplate.getForObject(instance.getUri() + "/hello?name=" + name, String.class);
//        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-client-provider");
//        ServiceInstance instance = balancer.getInstanceByCycle(instances);
//        return restTemplate.getForObject(instance.getUri() + "/hello?name=" + name, String.class);
//    }
    @GetMapping(value = "/hello")
    public String hello(@RequestParam String name) {
        return restTemplate.getForObject("http://eureka-client-provider/hello?name=" + name, String.class);
    }

}